<?php
/**
 *
 * @author Administrator
 * @version 
 */
require_once 'Zend/View/Interface.php';

/**
 * DebugInfo helper
 *
 * @uses viewHelper Zend_View_Helper
 */
class Zend_View_Helper_DebugInfo {
	
	/**
	 * @var Zend_View_Interface 
	 */
	public $view;
	
	private $_env;
	
	/**
	 * 调试信息输出
	 */
	public function debugInfo() {
		echo "<br><br>";
		echo "<p class='color_b'>--------------------我是非常碍眼的调试信息分割线----------------------------------------</p>";
//		$this->memcacheInfo();
		$this->dbInfo();
		try {
			$indexSearchPost = Zend_Registry::get('indexSearchPost');
			var_dump($indexSearchPost);
			$indexSearchTime = Zend_Registry::get('indexSearchTime');
			var_dump($indexSearchTime);
		}
		catch (Exception $e) {
			var_dump('No indexSearch!');
		}

		$this->phpInfo();
	}
	/**
	 * PHP执行时间
	 */
	public function phpInfo() {
		$fc = Zend_Controller_Front::getInstance();
		$this->_env = $fc->getPlugin('Initializer')->getEnv();
		if ('dev' == $this->_env || 'test' == $this->_env) {
			$s = new Zend_Session_Namespace('DebugInfo');
			$procTimeStart = $s->procTimeStart;				
			$procTimeEnd = microtime();
			$procTime = $procTimeEnd - $procTimeStart;
			echo "<br>PHP总计执行时间：" . $procTime*1000 . " ms<br>";
		}
		return null;
	}
	/**
	 * 数据库相关信息 
	 */
	public function dbInfo()	{
		$fc = Zend_Controller_Front::getInstance();
		$this->_env = $fc->getPlugin('Initializer')->getEnv();
		if ('dev' == $this->_env || 'test' == $this->_env) {
			$db = new DB();
			$qa = $db->getDbProfiler();
			echo "开发环境输出 => 数据库执行统计：	<br>\n";
			echo "SQL执行时间总计：" . $qa['sqlTimeTotal']*1000 . " ms<br>\n";
			echo "SQL执行次数总计：" . intval($qa['sqlCount']) . "<br>\n";
			echo "<br><br>\n";
			foreach ($qa as $k=>$q) {
				if (!is_array($q)) continue;
				echo "<div class='color_b'>SQL[" . $q['dbType'] . "][{$k}]：" . $q['sqlTime']*1000 . " ms</div>";
				echo "　　　　{$q['sqlText']}<br>";
				echo "\n";
			}
		}
		return null;
	}
	/**
	 * memcache相关信息输出
	 */
	public function memcacheInfo() {
		$fc = Zend_Controller_Front::getInstance();
		$this->_env = $fc->getPlugin('Initializer')->getEnv();
		if ('dev' == $this->_env || 'test' == $this->_env) {
			echo "开发环境输出 => memcache信息：	<br>\n";
			define('_MEMCACHE_HOST','192.168.10.254');
			define('_MEMCACHE_PORT',11211);
	    	$m = new Memcache;
			$m->addServer(_MEMCACHE_HOST,_MEMCACHE_PORT);
			$r = $m->getstats();
			if ($r) {
				echo 'ITEM：' . $r['curr_items'] . '/' . $r['total_items'] . '　';
				echo '命中未中：' . $r['get_hits'] . '/' . $r['get_misses'] . '　';
				echo '内存占用：' . round($r['bytes']/1024/1024,2) . '/' . round($r['limit_maxbytes']/1024/1024,2) . ' MB';
				echo "<br>\n";
//				var_dump($r);
			}
			else {
				echo "<p class='color_b'>Memcached服务失效：" . _MEMCACHE_HOST . ':' . _MEMCACHE_PORT . "</p>\n";
				echo "<br>\n";
			}
		}
		return null;
	}
	
	/**
	 * Sets the view field 
	 * @param $view Zend_View_Interface
	 */
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
	}
}
